package com.gxc.authcore.system.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.gxc.webcore.util.JsonUtil;
import com.gxc.webcore.util.RequestUtil;

public class ViewUtil {

	/**
	 * 构造跳转参数
	 * 
	 * @param request
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static String buildRedirectParam(HttpServletRequest request) {
		Map params = RequestUtil.getInMap(request);
		// 获取认证后的跳回上次的请求地址
		String clientRequestUrl = null;
		String url = request.getPathInfo() == null ? request.getServletPath()
				: request.getPathInfo();
		url = url.trim();
		while (url.startsWith("/")) {
			url = url.substring(1);
		}
		clientRequestUrl = ParamUtil.get("url_local") + "/" + url;
		params.put("clientRequestUrl", clientRequestUrl);
		// 客户端系统url
		String clientSystemUrl = ParamUtil.get("url_local");
		params.put("clientSystemUrl", clientSystemUrl);
		return SerializeUtil.serializeParam(params);
	}

	/**
	 * 跳转至登陆页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void redirectToLoginView() {
		HttpServletRequest request = CurrentUtil.getRequest();
		String requestMethod = request.getHeader("X-Requested-With");
		String jsonpCallback = request.getHeader("jsonpCallback");
		String loginUrl = ParamUtil.get("url_authcenter")
				+ "/common/auth/login.do";
		if (requestMethod != null && requestMethod.equals("XMLHttpRequest")) {
			// normal ajax request
			Map result = new HashMap();
			result.put("status", "failed");
			result.put("data", "您尚未登陆，或登陆已超时，<a href='" + loginUrl
					+ "'>请先登陆</a>。");
			String json_result = JsonUtil.object2json(result);
			try {
				CurrentUtil.getResponse().getWriter().write(json_result);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return;
		} else if (jsonpCallback != null && !jsonpCallback.isEmpty()) {
			// jsonp request
			Map result = new HashMap();
			result.put("status", "failed");
			result.put("data", "您尚未登陆，或登陆已超时，<a href='" + loginUrl
					+ "'>请先登陆</a>。");
			String json_result = JsonUtil.object2jsonp(result, jsonpCallback);
			try {
				CurrentUtil.getResponse().getWriter().write(json_result);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return;
		} else {
			// normal webpage request, redirect to login page
			try {
				CurrentUtil.getResponse().sendRedirect(
						loginUrl + buildRedirectParam(request));
			} catch (IOException e) {
				e.printStackTrace();
			}
			return;
		}
	}

	/**
	 * 跳转至权限提示页面
	 * 
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void redirectToNoAccessView() throws IOException {
		String requestMethod = CurrentUtil.getRequest().getHeader(
				"X-Requested-With");
		String jsonpCallback = CurrentUtil.getRequest().getHeader(
				"jsonpCallback");
		if (requestMethod != null && requestMethod.equals("XMLHttpRequest")) {
			// normal ajax request
			Map result = new HashMap();
			result.put("status", "failed");
			result.put("data", "您没有该请求的访问权限");
			String json_result = JsonUtil.object2json(result);
			CurrentUtil.getResponse().getWriter().write(json_result);
			return;
		} else if (jsonpCallback != null && !jsonpCallback.isEmpty()) {
			// jsonp request
			Map result = new HashMap();
			result.put("status", "failed");
			result.put("data", "您没有该请求的访问权限");
			String json_result = JsonUtil.object2jsonp(result, jsonpCallback);
			CurrentUtil.getResponse().getWriter().write(json_result);
			return;
		} else {
			// normal webpage request
			String url = ParamUtil.get("url_local")
					+ "/common/redirect/noAccess.do";
			CurrentUtil.getResponse().sendRedirect(url);
		}
	}
}
